﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using demo.BLL;
using demo.Common;
using demo.Entity;
using iAgileFramework.DataTransCarrier;

namespace WebApplication1
{
    public class ControllerBase : ControllerExtendBase
    {
        /// <summary>
        ///说明:授权过滤器
        ///作者:wly
        ///时间:2017-04-26 11:40:35
        /// </summary>
        /// <param name="filterContext">过滤内容</param>
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {

            #region 判断是否跳过授权验证

            object[] anonymousAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true);
            if (anonymousAttributes.Length > 0) return;
            #endregion

            #region 验证用户是否登录
            if (this.AccountUser == null)
            {
                filterContext.Result = new RedirectResult("~/Manage/Login/Index");
                return;
            }

            #endregion

            base.OnAuthorization(filterContext);
        }

        /// <summary>
        /// 说明:当前用户
        /// 作者：wly
        /// 时间：2017-04-26 11:42:21
        /// </summary>
        protected Users AccountUser
        {
            get
            {
                if (Context.user != null) return Context.user;
                var key = Cookie.getCookie(Context.CookieName);

                if (String.IsNullOrEmpty(key)) return null;

                string[] keys = DESEncrypt.Decrypt(key).Split('|');
                if (keys[1] != Utils.IPAddress) return null;

                //丢失重新获得
                ReturnValue rv = new UserBLL().DoLogin(keys[0], keys[1]);

                return !rv.State ? null : Context.user;
            }
        }
    }
}